package com.nonobank.architecture.demo;

import com.nonobank.architecture.schedule.AbstractTaskDealMulti;
import com.taobao.pamirs.schedule.TaskItemDefine;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 *
 */
@Component("dBDemoMulti")
public class DBDemoMulti extends AbstractTaskDealMulti<Long> {

	private Logger log = Logger.getLogger(DBDemoMulti.class);
	@Autowired
	private DataSource dataSource;

	public Comparator<Long> getComparator() {
		return new Comparator<Long>() {
			public int compare(Long o1, Long o2) {
				return o1.compareTo(o2);
			}

			public boolean equals(Object obj) {
				return this == obj;
			}
		};
	}

	/**
	 *
	 * @param taskParameter  锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷页锟斤拷锟斤拷锟矫的诧拷锟斤拷
	 * @param ownSign		锟斤拷锟斤拷
	 * @param taskItemNum	锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟?
	 * @param queryCondition  锟斤拷锟斤拷锟斤拷些锟斤拷锟斤拷锟斤拷
	 * @param fetchNum		每锟斤拷取锟斤拷锟斤拷锟斤拷锟斤拷
	 * @return
	 * @throws Exception
	 */
	public List<Long> queryTasks(String taskParameter, String ownSign, int taskItemNum,
								 List<TaskItemDefine> queryCondition, int fetchNum) throws Exception {
		System.out.println(queryCondition.toString());
		List<Long> result = new ArrayList<Long>();
		if (queryCondition.size() == 0) {
			return result;
		}

		StringBuffer condition = new StringBuffer();
		for (int i = 0; i < queryCondition.size(); i++) {
			if (i > 0) {
				condition.append(",");
			}
			condition.append(queryCondition.get(i).getTaskItemId());
		}

		Connection conn = null;
		try {
			conn = dataSource.getConnection();

			String sql = "select ID from SCHEDULE_TEST where mod(id," + taskItemNum + ") in ("
					+ condition.toString() + ") and sts ='N' LIMIT "
					+ fetchNum;

			PreparedStatement statement = conn.prepareStatement(sql);
			ResultSet set = statement.executeQuery();
			while (set.next()) {
				result.add(set.getLong("ID"));
			}
			set.close();
			statement.close();
			return result;
		} finally {
			if (conn != null)
				conn.close();
		}
	}



	public boolean handleTask(Long[] tasks, String ownSign) throws Exception {
		Connection conn = null;
		long id = 0;
		try {
			conn = dataSource.getConnection();
			conn.setAutoCommit(false);
			for (int index = 0; index < tasks.length; index++) {
				id = ((Long) tasks[index]).longValue();
				log.debug("锟斤拷锟斤拷锟斤拷锟斤拷" + id + " 锟缴癸拷锟斤拷");
				String sql = "update SCHEDULE_TEST SET STS ='Y' ,DEAL_COUNT = DEAL_COUNT + 1 WHERE ID = ? and STS ='N' ";
				PreparedStatement statement = conn.prepareStatement(sql);
				statement.setLong(1, id);
				statement.executeUpdate();
				statement.close();
			}
			conn.commit();
		} catch (Exception e) {
			log.error("执锟斤拷锟斤拷锟斤拷" + id + "失锟杰ｏ拷" + e.getMessage(), e);
			if (conn != null) {
				conn.rollback();
			}
			return false;
		} finally {
			if (conn != null) {
				conn.close();
			}
		}
		return true;
	}

}